package edu.bu.cs673.superkids123.common.view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import edu.bu.cs673.superkids123.AbstractFrame;
import edu.bu.cs673.superkids123.AbstractNavigationView;
import edu.bu.cs673.superkids123.common.controller.ParentsLoginController;
import edu.bu.cs673.superkids123.common.controller.exceptions.AuthException;

public class ParentsLoginView extends AbstractNavigationView implements
		ActionListener {

	private JButton btnLogin;
	private JTextField txtUsername;
	private JPasswordField txtPassword;
	private JLabel lblError;

	public ParentsLoginView(AbstractFrame mainFrame) {
		super(mainFrame);
	}

	/**
	 * @wbp.parser.entryPoint
	 */
	@Override
	protected JPanel layout() {

		this.setParentsButtonVisible(false);

		JPanel panel = new JPanel();
		GridBagLayout gbl_panel = new GridBagLayout();
		gbl_panel.columnWidths = new int[] { 75, 100, 20, 300, 75 };
		gbl_panel.rowHeights = new int[] { 50, 10, 50, 200 };
		panel.setLayout(gbl_panel);

		JLabel lblUsername = new JLabel("Username:");
		lblUsername.setFont(new Font("Comic Sans MS", Font.BOLD, 16));
		GridBagConstraints gbc_lblUsername = new GridBagConstraints();
		gbc_lblUsername.insets = new Insets(0, 0, 5, 5);
		gbc_lblUsername.anchor = GridBagConstraints.EAST;
		gbc_lblUsername.gridx = 1;
		gbc_lblUsername.gridy = 0;
		panel.add(lblUsername, gbc_lblUsername);

		txtUsername = new JTextField();
		txtUsername.setFont(new Font("Comic Sans MS", Font.BOLD, 16));
		GridBagConstraints gbc_txtUsername = new GridBagConstraints();
		gbc_txtUsername.insets = new Insets(0, 0, 5, 5);
		gbc_txtUsername.fill = GridBagConstraints.HORIZONTAL;
		gbc_txtUsername.gridx = 3;
		gbc_txtUsername.gridy = 0;
		panel.add(txtUsername, gbc_txtUsername);
		txtUsername.setColumns(10);

		JLabel lblPassword = new JLabel("Password:");
		lblPassword.setFont(new Font("Comic Sans MS", Font.BOLD, 16));
		GridBagConstraints gbc_lblPassword = new GridBagConstraints();
		gbc_lblPassword.insets = new Insets(0, 0, 5, 5);
		gbc_lblPassword.anchor = GridBagConstraints.EAST;
		gbc_lblPassword.gridx = 1;
		gbc_lblPassword.gridy = 2;
		panel.add(lblPassword, gbc_lblPassword);

		txtPassword = new JPasswordField();
		txtPassword.setFont(new Font("Comic Sans MS", Font.BOLD, 16));
		GridBagConstraints gbc_txtPassword = new GridBagConstraints();
		gbc_txtPassword.insets = new Insets(0, 0, 5, 5);
		gbc_txtPassword.fill = GridBagConstraints.HORIZONTAL;
		gbc_txtPassword.gridx = 3;
		gbc_txtPassword.gridy = 2;
		panel.add(txtPassword, gbc_txtPassword);
		txtPassword.setColumns(10);

		JPanel loginPanel = new JPanel();
		GridBagConstraints gbc_loginPanel = new GridBagConstraints();
		gbc_loginPanel.anchor = GridBagConstraints.EAST;
		gbc_loginPanel.insets = new Insets(0, 0, 0, 5);
		gbc_loginPanel.fill = GridBagConstraints.VERTICAL;
		gbc_loginPanel.gridx = 1;
		gbc_loginPanel.gridy = 3;
		gbc_loginPanel.gridwidth = 3;
		panel.add(loginPanel, gbc_loginPanel);

		lblError = new JLabel("");
		lblError.setForeground(Color.RED);
		lblError.setFont(new Font("Comic Sans MS", Font.BOLD, 16));
		loginPanel.add(lblError, BorderLayout.WEST);

		btnLogin = new JButton("Login");
		btnLogin.addActionListener(this);
		btnLogin.setFont(new Font("Comic Sans MS", Font.BOLD, 16));
		loginPanel.add(btnLogin, BorderLayout.EAST);

		return panel;
	}

	@Override
	public void actionPerformed(ActionEvent event) {
		if (event.getSource().equals(btnLogin)) {
			try {
				getMainFrame().getController(ParentsLoginController.class)
						.login(txtUsername.getText(),
								new String(txtPassword.getPassword()));
			} catch (AuthException exception) {
				lblError.setText("Invalid username or password!");
			}
		}

	}

	@Override
	public String getTitle() {
		return "Parent's Console Login";
	}

	@Override
	public String getBorderTitle() {
		return "Please authenticate";
	}
}
